package chatManagers;

//package wmmd.chat;
//package chatManagers;

/*
 * getChatUsers.java
 *
 * Created on January 10, 2007
 *
 * Retrieves the list of chat users from the chatUsers table; called by
 * ManageUsers.jsp in the administrative section of the website.
 */

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class getChatUsers extends HttpServlet {
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/plain");
        response.setHeader("Cache-control", "no-cache");
        PrintWriter out = response.getWriter();
        
        String responseString = new String();
        DBMSManager dbm = new DBMSManager();
        ResultSet rs = null;
        
        // Return an HTML formatted list of all active chat members
        try {
            rs = dbm.getMembers(0);
            if(rs != null)
            {
                responseString += printAllChatters(rs);
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace(out);
        }
        finally
        {dbm.closeConnection();}
        
        //System.out.print(responseString);
        out.print(responseString);
    }
       
    public String printAllChatters(ResultSet r) {
        String s = "";
        try {
            while (r.next()) {
                    String uid = r.getString("ID");
                    String user = r.getString("username");
                    s += "<option id=\"" + uid + "\" value=\"" + user + "\">" + user + "</option>";
                }
        } catch (SQLException se) {
            se.printStackTrace();
        }
        return s;
    }
    
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
// </editor-fold>
}
